package com.liuguang.realgateway.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.support.RouterFunctionMapping;
import reactor.core.publisher.Mono;

/**
 * @ClassName: MyTestHandler
 * @Description： 测试 webFlux 中自定义handler 使某特定路径访问经过该处理器处理，直接返回前端，不在去调用内部微服务
 * @Author: wanghr
 * Date: 3:48 PM 2020/10/30
 * Version: 1.0
 */
//@Component
public class MyTestHandler implements HandlerFunction<ServerResponse> {
    private static final Logger logger = LoggerFactory.getLogger(MyTestHandler.class);

    /**
     * 将一个 serverRequest 转换为 ServerResponse
     * 实际上就是相当于处理了请求，返回结果。
     */
    @Override
    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
        String path = serverRequest.path();
        logger.info("请求的path：{}", path);
        //模拟生成验证码
        String validateCode = null;
        try {
            validateCode = "qwer";
        } catch (Exception e) {
            logger.info("获取验证码的过程中出现异常");
            return Mono.error(e);
        }
        return ServerResponse.ok().body(BodyInserters.fromValue(validateCode));
    }
}
